package com.hetang.maijiayun.adaptertools.adapter;

import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.domain.Trade;
import com.taobao.api.request.TradesSoldIncrementGetRequest;
import com.taobao.api.response.TradesSoldIncrementGetResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wanda on 11/4/15.
 */
public class TmallAdapter extends AdapterAbstract {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(TmallAdapter.class);

    @Override
    public Integer request(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        return null;
    }

    @Override
    public List<String> requestOrders(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        if (MapUtils.isEmpty(authorizesMap) || !authorizesMap.containsKey("url") || !authorizesMap.containsKey("appKey") || !authorizesMap.containsKey("secret") || !authorizesMap.containsKey("sessionKey")) {
            logger.error("该天猫平台店铺没有授权信息或授权信息错误！！");
            return null;
        }
        DefaultTaobaoClient client = new DefaultTaobaoClient(authorizesMap.get("url"), authorizesMap.get("appKey"), authorizesMap.get("secret"));
        TradesSoldIncrementGetRequest req = new TradesSoldIncrementGetRequest();
        req.setFields("tid, total_results");
        req.setPageSize(50L);
        req.setPageNo(1L);
        req.setTimestamp(System.currentTimeMillis());
        req.setStartModified(request.getStartTime());
        req.setEndModified(request.getEndTime());
        logger.info("tmall get order interface, request time is : {}, request param is : {}", System.currentTimeMillis(), JsonTools.createJsonString(req));
        TradesSoldIncrementGetResponse rsp = client.execute(req, authorizesMap.get("sessionKey"));
        logger.info("tmall get order interface, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
        if (rsp == null || !"".equals(rsp.getErrorCode())) {
            return null;
        }
        Long page = 0L;
        Long mod = rsp.getTotalResults()%50L;
        page = rsp.getTotalResults()/50L;
        if (0 != mod) {
            page += 1;
        }
        List<String> result = new ArrayList<String>();
        while (page > 0) {
            req.setPageNo(page);
            rsp = client.execute(req, authorizesMap.get("sessionKey"));
            if (!"".equals(rsp.getErrorCode())) {
                logger.error("tmall get order interface error, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
                continue;
            }
            if (CollectionUtils.isEmpty(rsp.getTrades())) {
                continue;
            }
            for (Trade trade : rsp.getTrades()) {
                result.add(trade.getTid() + "");
            }
            page -- ;
        }
        return result;
    }
}
